{include file="common/head"/}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>手动发放优惠券</legend>
    </fieldset>
    <form class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <label class="layui-form-label">会员手机号</label>
            <div class="layui-input-4">
                <input type="text" maxlength="200" name="phone" value="" lay-verify="required|phone"
                    placeholder="请输入会员手机号" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">优惠券分类</label>
            <div class="layui-input-inline">
                <select name="cate_id" lay-filter="category" lay-search placeholder="请选择优惠券分类" lay-verify="category"
                    id="category">
                    {volist name="list_category" id="vo"}
                    <option value="{$vo.id}">{$vo.title}</option>
                    {/volist}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">优惠券</label>
            <div class="layui-input-inline">
                <select name="coupon_id" lay-filter="coupon_id" lay-search placeholder="请选择优惠券" lay-verify="coupon"
                    id="coupon_id">
                    <option>请选择优惠券</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="submit" lay-submit=""
                    lay-filter="sys">{:lang('submit')}</button>
            </div>
        </div>
    </form>
</div>
{include file="common/foot"/}
<script>
    layui.use(['form', 'layer', 'upload'], function () {
        var form = layui.form, layer = layui.layer, upload = layui.upload, $ = layui.jquery;
        form.verify({
            title: function (value) {
                if (value == '') {
                    return '请输入优惠券分类名称!';
                }
            },
            category: function (value) {
                if (value == '') {
                    return '请选择优惠券分类!';
                }
            },
            coupon: function (value) {
                if (value == '') {
                    return '请选择优惠券!';
                }
            }



            
        });
        //提交监听
        form.on('select(category)', function (obj) {
            let val = obj.value
            $.post('{:url("getcoupon")}', { cate_id: val }, function (res) {
                let data = res.data
                if (data.length > 0) {
                    let leng = data.length
                    let text = ` <option >请选择优惠券</option>`
                    for (let i = 0; i < leng; i++) {
                        $('#coupon_id').append(new Option(data[i].title, data[i].id))
                        text += ` <option value="${data[i].id}">${data[i].title}</option>`
                    }

                    form.render();
                }

            })

        });
        form.on('submit(sys)', function (data) {
            $("#submit").attr({ disabled: "disabled" });
            loading = layer.load(1, { shade: [0.1, '#fff'] });
            $.post("{:url('userAdd')}", data.field, function (res) {
                layer.close(loading);
                if (res.errorCode == 8888) {
                    layer.msg(res.msg, { icon: 1, time: 1000 }, function () {
                        location.href = '{:url("user")}';
                    });
                } else {
                    $("#submit").removeAttr("disabled");
                    layer.msg(res.msg, { icon: 2, time: 1000 });
                }
            });
        })
    })


</script>


</body>

</html>